package com.ipzoe.web.controller.mem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.mem.domain.MemberRechargePackage;
import com.ipzoe.mem.service.IMemberRechargePackageService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 会员充值套餐Controller
 * 
 * @author eric
 * @date 2020-08-20
 */
@RestController
@RequestMapping("/member/rechargePackage" )
public class MemberRechargePackageController extends BaseController {

    @Autowired
    private IMemberRechargePackageService iMemberRechargePackageService;

    /**
     * 查询会员充值套餐列表
     */
    @PreAuthorize("@ss.hasPermi('member:rechargePackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberRechargePackage memberRechargePackage)
    {
        startPage();
        LambdaQueryWrapper<MemberRechargePackage> lqw = new LambdaQueryWrapper<MemberRechargePackage>();
        List<MemberRechargePackage> list = iMemberRechargePackageService.list(lqw);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('member:rechargePackage:list')")
    @GetMapping("/listAllEnabled")
    public AjaxResult listAllEnabled()
    {
        LambdaQueryWrapper<MemberRechargePackage> lqw = new LambdaQueryWrapper<MemberRechargePackage>();
        lqw.ge(MemberRechargePackage::getExpiryDay,new Date());
        return AjaxResult.success(iMemberRechargePackageService.list(lqw));
    }



    /**
     * 导出会员充值套餐列表
     */
    @PreAuthorize("@ss.hasPermi('member:rechargePackage:export')" )
    @Log(title = "会员充值套餐" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MemberRechargePackage memberRechargePackage) {
        LambdaQueryWrapper<MemberRechargePackage> lqw = new LambdaQueryWrapper<MemberRechargePackage>(memberRechargePackage);
        List<MemberRechargePackage> list = iMemberRechargePackageService.list(lqw);
        ExcelUtil<MemberRechargePackage> util = new ExcelUtil<MemberRechargePackage>(MemberRechargePackage. class);
        return util.exportExcel(list, "rechargePackage" );
    }

    /**
     * 获取会员充值套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:rechargePackage:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMemberRechargePackageService.getById(id));
    }

    /**
     * 新增会员充值套餐
     */
    @PreAuthorize("@ss.hasPermi('member:rechargePackage:add')" )
    @Log(title = "会员充值套餐" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberRechargePackage memberRechargePackage) {
        memberRechargePackage.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iMemberRechargePackageService.save(memberRechargePackage) ? 1 : 0);
    }

    /**
     * 修改会员充值套餐
     */
    @PreAuthorize("@ss.hasPermi('member:rechargePackage:edit')" )
    @Log(title = "会员充值套餐" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberRechargePackage memberRechargePackage) {
        memberRechargePackage.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iMemberRechargePackageService.updateById(memberRechargePackage) ? 1 : 0);
    }

    /**
     * 删除会员充值套餐
     */
    @PreAuthorize("@ss.hasPermi('member:rechargePackage:remove')" )
    @Log(title = "会员充值套餐" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMemberRechargePackageService.removeByIds(Arrays.asList(ids),SecurityUtils.getUsername()) ? 1 : 0);
    }
}
